package grape.sequence.service.generator.periodparser;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import grape.sequence.service.config.po.Config;
import org.springframework.stereotype.Component;

import java.util.Date;

import static cn.hutool.core.date.DatePattern.PURE_DATE_PATTERN;

/**
 * Created by yangwei
 * Created at 2020/8/20 18:58
 */
@Component
public class DefaultPeriodUnitMonthParser  implements IPeriodUnitParser{
    private static String prefix = "month_";
    private static String format_pattern = "yyyyMM";
    @Override
    public ParserResult parse(Config config,Config.PeriodUnitDictItem periodUnitDictItem) {
        if(periodUnitDictItem == Config.PeriodUnitDictItem.period_unit_month){
            Date now = ParserHelper.getNow();
            // 周期不为1，计算周期
            Date start = ParserHelper.getStart(config);
            long intervals = DateUtil.betweenMonth(start, now, true);
            long mod = intervals / config.getPeriod();
            DateTime dateTime = DateUtil.offsetMonth(start, ((int) mod));
            return new ParserResult(prefix, DateUtil.format(dateTime, format_pattern));

        }
        return null;
    }
}
